<?php
/**
 * Created by PhpStorm.
 * User: envision
 * Date: 2018-10-08
 * Time: 12:55
 */

namespace App\Http\Controllers\Api\V1\Manager\User;


use App\Http\Controllers\Controller;
use App\Models\UserManager;
use App\Models\UserManagerRole;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Facades\Hash;

class ManagerController extends Controller
{
    public function index(Request $request)
    {
        $keyWords = $request->keyWords;
        $pageSize = $request->pageSize;

        $sql = UserManager::select(
                                'managerId',
                                'accessToken',
                                'name',
                                'userName',
                                'roleName',
                                'status',
                                'createTime'
                            );
                        
        if(isset($keyWords)){
            $sql->where(function($query) use ($keyWords){
                $query->where('userName','like','%'.$keyWords.'%')->orWhere('name','like','%'.$keyWords.'%');
            });
            
        }
        $manager = $sql->where('isPlant',1)->orderBy('updateTime','desc')->paginate($pageSize)->toArray();
        foreach ($manager['data'] as &$value) {
            $value['roleName'] = explode(';',$value['roleName']);
        }
        $data['managerList'] = $manager['data'];
        $data['totalNum'] = $manager['total'];
        return $this->apiResponse($data);
    }

    /**
     * 新增修改管理员
     * @param  Request $request [description]
     * @return [type]           [description]
     */
    public function store(Request $request)
    {
        $managerId = $request->managerId;
        $roleName = $request->roleName;
        $roleName = implode(';',$roleName);
        $data = $request->only('name','userName');
        $data['roleName'] = $roleName;

        if($managerId){
            $num = UserManager::where('userName',$data['userName'])->where('managerId','!=',$managerId)->count();
            if($num){
                return $this->apiResponse('', config('errorLiu.ADMINUSERNAMEBUKEYICHONGFU'));
            }
            UserManager::where('managerId',$managerId)->update($data);
        }else{
            $num = UserManager::where('userName',$data['userName'])->count();
            if($num){
                return $this->apiResponse('', config('errorLiu.ADMINUSERNAMEBUKEYICHONGFU'));
            }
            $time = date("Y-m-d H:i:s");
            $data['password'] = Hash::make(888888);
            $data['createTime'] = $time;
            $data['updateTime'] = $time;
            $data['isPlant'] = 1;
            $data['accessToken'] = encrypt($data['userName'] . time());

            UserManager::insert($data);
        }
        return $this->apiResponse(true);
    }

    /**
     * 新管理员禁用开启
     * @param  Request $request [description]
     * @return [type]           [description]
     */
    public function adminStatus($managerId)
    {
        $status = UserManager::where('managerId',$managerId)->value('status');
        $userName = UserManager::where('managerId',$managerId)->value('userName');
        $data = [];
        $data['updateTime'] = date("Y-m-d H:i:s");
        $data['accessToken'] = encrypt($userName . time());
        if($status){
            $data['status'] = 0;
            UserManager::where('managerId',$managerId)->update($data);
        }else{
            $data['status'] = 1;
            UserManager::where('managerId',$managerId)->update($data);
        }
        return $this->apiResponse(true);
    }

    /**
     * 管理员删除
     * @param  Request $request [description]
     * @return [type]           [description]
     */
    public function delete($managerId)
    {
        UserManager::where('managerId',$managerId)->where('isPlant',1)->delete();
        return $this->apiResponse(true);
    }

    /**
     * 管理员修改密码
     * @param  Request $request [description]
     * @return [type]           [description]
     */
    public function updatePassword(Request $request)
    {
        $oldPassword = $request->oldPassword;
        $newPassword = $request->newPassword;
        $managerId = $request->managerId;

        $password = UserManager::where('managerId',$managerId)->value('password');
        if(!Hash::check($oldPassword, $password)){
            return $this->apiResponse('', config('errorLiu.OLDPASSWORDCUOWUCHONGSHU'));
        }

        $userName = UserManager::where('managerId',$managerId)->value('userName');
        $data = [];
        $data['password'] = Hash::make($newPassword);
        $data['updateTime'] = date("Y-m-d H:i:s");
        $data['accessToken'] = encrypt($userName . time());
        UserManager::where('managerId',$managerId)->update($data);
        return $this->apiResponse(true);
    }

    /**
     * 管理员重置密码
     * @param  Request $request [description]
     * @return [type]           [description]
     */
    public function resetPassword($managerId)
    {
        $userName = UserManager::where('managerId',$managerId)->value('userName');
        $data = [];
        $data['password'] = Hash::make(888888);
        $data['updateTime'] = date("Y-m-d H:i:s");
        $data['accessToken'] = encrypt($userName . time());
        UserManager::where('managerId',$managerId)->update($data);
        return $this->apiResponse(true);
    }
}